package com.g1.screens.model;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

import com.g1.screens.enumerations.BlockType;

public class Block {
	
	private Image image;
	private float x;
	private float y;
	private int column;
	private int row;
	private BlockType type;
	
	private boolean movable;
	private boolean isSelected;
	private boolean toRemove;
	private boolean falling;
	
	private float deltaX;
	private float deltaY;
	

	public Block(final int column, final int row, final BlockType type){
		this.column = column;
		this.row = row;
		this.setType(type);
		this.movable = true;		
	}
	
	public Block(final int column, final int row, final BlockType type, Image blocksResource){
		this.column = column;
		this.row = row;
		this.movable = true;
		this.image = type.getImage(blocksResource) ;
		this.setType(type);
	}
	
	public Image getImage() {
		return image;
	}

	public void setImage(Image image) {
		this.image = image;
	}

	public float getX() {
		return x;
	}

	public void setX(float x) {
		this.x = x;
	}

	public float getY() {
		return y;
	}

	public void setY(float y) {
		this.y = y;
	}

	public int getColumn() {
		return column;
	}

	public void setColumn(int column) {
		this.column = column;
	}

	public int getRow() {
		return row;
	}

	public void setRow(int row) {
		this.row = row;
	}

	public BlockType getType() {
		return type;
	}

	public void setType(BlockType type) {
		this.type = type;
	}

	public void setMovable(boolean movable) {
		this.movable = movable;
	}

	public boolean isMovable() {
		return movable;
	}
	
	public void render(GameContainer container, StateBasedGame game, Graphics g)
	throws SlickException {
		if(image!=null) {
			g.drawImage(image, x, y, (isSelected?Color.blue:Color.white));
			g.drawString("["+column+","+row+"]", x, y+BlockType.height/2);
		}
	}

	public void setSelected(boolean isSelected) {
		this.isSelected = isSelected;
	}

	public boolean isSelected() {
		return isSelected;
	}

	public void setToRemove(boolean toRemove) {
		this.toRemove = toRemove;
	}
	
	public boolean isToRemove() {
		return toRemove;
	}
	
	public float moveDeltaX(final float deltaX){
		this.deltaX += deltaX;
		return x += deltaX;
	}
	
	public float moveDeltaY(final float deltaY){
		this.deltaY += deltaY;
		return y += deltaY;
	}
	
	public boolean isReachPosition(){
		boolean result = this.deltaX >= BlockType.width || this.deltaY >= BlockType.height ;
		if(result) resetDelta();
		return result;
	}
	
	public void resetDelta(){
		this.deltaX = 0 ;
		this.deltaY = 0 ;
	}

	public boolean isFalling() {
		return falling;
	}

	public void setFalling(boolean falling) {
		this.falling = falling;
	}
	
 
	

}
